package com.seres.backend.admin;

import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;

import com.seres.data.Index;

public class IndicesParser {
	private static final int MAX_NR_OF_INDICES = 5;
	
	private static final int ID_COL_NR = 1;
	private static final int INDEX_NAME_COL_NR = 2;
	private static final int MIN_STATUS_VALUE_COL_NR = 3;
	private static final int MAX_STATUS_VALUE_COL_NR = 4;
	private static final int RESET_STATUS_VALUE_COL_NR = 5;
	private static final int INITIAL_STATUS_VALUE_COL_NR = 6;
	private static final int THRESHOLD_STATUS_VALUE_COL_NR = 7;	
	
	private static final int TITLE_COL_NR = 1;
	private static final int TITLE_ROW_NR = 1;
	private static final String TITLE = "indices";
	
	private static final int DATA_START_ROW_NR = 3;
	
	private boolean dataEndReached = false;
	
	private XSSFSheet worksheet;
	
	public IndicesParser(XSSFSheet worksheet){
		setWorksheet(worksheet);
	}
	
	public IndicesParserResult parse(){
		IndicesParserResult result = new IndicesParserResult();
		
		for(int i = 0; i < MAX_NR_OF_INDICES && !isDataEndReached(); i++){
			XSSFRow row = getWorksheet().getRow(DATA_START_ROW_NR + i);
			
			if(containsData(row)){
				int id = parseId(row.getCell(ID_COL_NR));
				String indexName = parseIndexName(row.getCell(INDEX_NAME_COL_NR));
				float minStatusValue = parseMinStatusValue(row.getCell(MIN_STATUS_VALUE_COL_NR));
				float maxStatusValue = parseMaxStatusValue(row.getCell(MAX_STATUS_VALUE_COL_NR));
				float resetStatusValue = parseResetStatusValue(row.getCell(RESET_STATUS_VALUE_COL_NR));
				float initialStatusValue = parseInitialStatusValue(row.getCell(INITIAL_STATUS_VALUE_COL_NR));
				float thresholdStatusValue = parseThresholdStatusValue(row.getCell(THRESHOLD_STATUS_VALUE_COL_NR));
				
				System.out.println("row " + (DATA_START_ROW_NR + i));
				System.out.println("*************");
				System.out.println("id: " + id);
				System.out.println("indexName: " + indexName);
				System.out.println("minStatusValue: " + minStatusValue);
				System.out.println("maxStatusValue: " + maxStatusValue);
				System.out.println("resetStatusValue: " + resetStatusValue);
				System.out.println("initialStatusValue: " + initialStatusValue);
				System.out.println("thresholdStatusValue: " + thresholdStatusValue);
				System.out.println("");
				
				result.addIndex(id, new Index(indexName, minStatusValue, maxStatusValue, resetStatusValue, initialStatusValue, thresholdStatusValue));
			}
			else{
				setDataEndReached(true);
			}

		}
		return result;
		
	}
	private int parseId(XSSFCell cell){
		if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
			return (int) cell.getNumericCellValue();
		}
		else{
			throw new IllegalArgumentException("invalid ID cell");
		}
	}
	private String parseIndexName(XSSFCell cell){
		if(cell.getCellType() == Cell.CELL_TYPE_STRING){
			return cell.getStringCellValue();
		}
		else{
			throw new IllegalArgumentException("invalid index name cell");
		}
	}
	private float parseMinStatusValue(XSSFCell cell){
		if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
			return (float) cell.getNumericCellValue();
		}
		else{
			throw new IllegalArgumentException("invalid min status value cell");
		}
	}
	private float parseMaxStatusValue(XSSFCell cell){
		if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
			return (float) cell.getNumericCellValue();
		}
		else{
			throw new IllegalArgumentException("invalid max status value cell");
		}
	}
	private float parseResetStatusValue(XSSFCell cell){
		if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
			return (float) cell.getNumericCellValue();
		}
		else{
			throw new IllegalArgumentException("invalid reset status value cell");
		}
	}
	private float parseInitialStatusValue(XSSFCell cell){
		if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
			return (float) cell.getNumericCellValue();
		}
		else{
			throw new IllegalArgumentException("invalid initial status value cell");
		}
	}
	private float parseThresholdStatusValue(XSSFCell cell){
		if(cell.getCellType() == Cell.CELL_TYPE_NUMERIC){
			return (float) cell.getNumericCellValue();
		}
		else{
			throw new IllegalArgumentException("invalid threshold status value cell");
		}
	}
	private boolean containsData(XSSFRow row){
		return row.getCell(ID_COL_NR).getCellType() != Cell.CELL_TYPE_BLANK;
	}
	
	private XSSFSheet getWorksheet() {
		return worksheet;
	}
	public boolean canHaveAsWorksheet(XSSFSheet worksheet){
		return worksheet.getRow(TITLE_ROW_NR).getCell(TITLE_COL_NR).getStringCellValue().toLowerCase().equals(TITLE);
	}
	private void setWorksheet(XSSFSheet worksheet) {
		if(canHaveAsWorksheet(worksheet)){
			this.worksheet = worksheet;
		}
		else{
			throw new IllegalArgumentException("invalid indices worksheet");
		}
		
	}
	private boolean isDataEndReached() {
		return dataEndReached;
	}
	private void setDataEndReached(boolean dataEndReached) {
		this.dataEndReached = dataEndReached;
	}
}
